package com.ecar.api.support.util;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.XppDriver;
import net.sf.json.JSONObject;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

/** @noinspection unused*/
public class WechatXMLParser {

	private static final Logger log = LoggerFactory.getLogger(WechatXMLParser.class);

	public static XStream xstream = new XStream(new XppDriver() {
		public HierarchicalStreamWriter createWriter(Writer out) {
			return new PrettyPrintWriter(out) {
				boolean cdata = true;

				public void startNode(String name, Class clazz) {
					super.startNode(name, clazz);
				}

				protected void writeText(QuickWriter writer, String text) {
					if (cdata) {
						writer.write("<![CDATA[");
						writer.write(text);
						writer.write("]]>");
					} else {
						writer.write(text);
					}
				}
			};
		}
	});

	public static Map<String, String> parseXML(HttpServletRequest request)
			throws IOException, DocumentException {
		Map<String, String> messageMap = new HashMap<>();

		InputStream input = request.getInputStream();

		SAXReader reader = new SAXReader();
		Document doc = reader.read(input);
		Element root = doc.getRootElement();
		for (Element e : (List<Element>) root.elements()) {
			messageMap.put(e.getName(), e.getText());
		}

		input.close();
		return messageMap;
	}

	public static JSONObject parseXMLToJson(HttpServletRequest request)
			throws IOException, DocumentException {
		JSONObject messageMap = new JSONObject();

		InputStream input = request.getInputStream();

		SAXReader reader = new SAXReader();
		Document doc = reader.read(input);
		Element root = doc.getRootElement();
		for (Element e : (List<Element>) root.elements()) {
			messageMap.put(e.getName(), e.getText());
		}

		input.close();
		return messageMap;
	}

	public static Map<String, String> parseString(String message)
			throws IOException, DocumentException {
		SortedMap<String, String> messageMap = new TreeMap<>();

		Document doc = DocumentHelper.parseText(message);
		Element root = doc.getRootElement();
		for (Element e : (List<Element>) root.elements()) {
			messageMap.put(e.getName(), e.getText());
		}

		return messageMap;
	}

	public static String map2XmlString(Map<String, String> map) {
		StringBuilder sb = new StringBuilder();
		sb.append("<xml>");
		for (String key : map.keySet()) {

			String value = "<![CDATA[" + map.get(key) + "]]>";
			sb.append("<").append(key).append(">")
			  .append(value)
			  .append("</").append(key).append(">");
		}
		sb.append("</xml>");
		return sb.toString();
	}

}
